/**
 * 验证码工具类
 */
class CaptchaUtil {
  /**
   * 生成随机颜色
   * @private
   */
  static _randomColor(min, max) {
    const r = this._randomNum(min, max);
    const g = this._randomNum(min, max);
    const b = this._randomNum(min, max);
    return `rgb(${r},${g},${b})`;
  }

  /**
   * 生成随机数字
   * @private
   */
  static _randomNum(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
  }

  /**
   * 生成验证码
   * @param {Object} options - 配置选项
   * @param {number} options.width - 验证码宽度
   * @param {number} options.height - 验证码高度
   * @param {number} options.length - 验证码长度
   * @returns {Object} 包含验证码文本和canvas元素的对象
   */
  static generate(options = {}) {
    const defaultOptions = {
      width: 120,
      height: 40,
      length: 4
    };

    const finalOptions = { ...defaultOptions, ...options };
    const { width, height, length } = finalOptions;

    // 创建canvas元素
    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext('2d');

    // 设置背景色
    ctx.fillStyle = '#f6f6f6';
    ctx.fillRect(0, 0, width, height);

    // 定义验证码字符集
    const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    let code = '';

    // 绘制字符
    for (let i = 0; i < length; i++) {
      const char = chars.charAt(Math.floor(Math.random() * chars.length));
      code += char;
      
      ctx.font = this._randomNum(28, 33) + 'px SimHei';
      ctx.textBaseline = 'middle';
      ctx.fillStyle = this._randomColor(50, 160);
      
      const x = 10 + i * (width - 20) / length;
      const y = height / 2;
      const rotate = this._randomNum(-30, 30) * Math.PI / 180;
      
      ctx.translate(x, y);
      ctx.rotate(rotate);
      ctx.fillText(char, 0, 0);
      ctx.rotate(-rotate);
      ctx.translate(-x, -y);
    }

    // 绘制干扰线
    for (let i = 0; i < 4; i++) {
      ctx.strokeStyle = this._randomColor(40, 180);
      ctx.beginPath();
      ctx.moveTo(this._randomNum(0, width), this._randomNum(0, height));
      ctx.lineTo(this._randomNum(0, width), this._randomNum(0, height));
      ctx.stroke();
    }

    // 绘制干扰点
    for (let i = 0; i < 20; i++) {
      ctx.fillStyle = this._randomColor(0, 255);
      ctx.beginPath();
      ctx.arc(this._randomNum(0, width), this._randomNum(0, height), 1, 0, 2 * Math.PI);
      ctx.fill();
    }

    return {
      text: code,           // 验证码文本
      data: canvas.toDataURL('image/png'), // 验证码图片（base64格式）
      canvas: canvas        // canvas元素
    };
  }

  /**
   * 生成算术验证码
   * @param {Object} options - 配置选项
   * @returns {Object} 包含验证码文本、结果和canvas元素的对象
   */
  static generateMath(options = {}) {
    const defaultOptions = {
      width: 120,
      height: 40
    };

    const finalOptions = { ...defaultOptions, ...options };
    const { width, height } = finalOptions;

    // 生成算术表达式
    const num1 = this._randomNum(1, 20);
    const num2 = this._randomNum(1, 20);
    const operators = ['+', '-', '×'];
    const operator = operators[this._randomNum(0, operators.length)];
    
    let result;
    switch (operator) {
      case '+':
        result = num1 + num2;
        break;
      case '-':
        result = num1 - num2;
        break;
      case '×':
        result = num1 * num2;
        break;
    }

    const expression = `${num1}${operator}${num2}=?`;

    // 创建canvas元素
    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext('2d');

    // 设置背景色
    ctx.fillStyle = '#f6f6f6';
    ctx.fillRect(0, 0, width, height);

    // 绘制表达式
    ctx.font = '24px SimHei';
    ctx.textBaseline = 'middle';
    ctx.textAlign = 'center';
    ctx.fillStyle = this._randomColor(50, 160);
    ctx.fillText(expression, width / 2, height / 2);

    // 绘制干扰线
    for (let i = 0; i < 3; i++) {
      ctx.strokeStyle = this._randomColor(40, 180);
      ctx.beginPath();
      ctx.moveTo(this._randomNum(0, width), this._randomNum(0, height));
      ctx.lineTo(this._randomNum(0, width), this._randomNum(0, height));
      ctx.stroke();
    }

    return {
      text: expression,    // 验证码文本
      result: result.toString(), // 计算结果
      data: canvas.toDataURL('image/png'), // 验证码图片（base64格式）
      canvas: canvas       // canvas元素
    };
  }

  /**
   * 验证用户输入是否正确
   * @param {string} userInput - 用户输入的验证码
   * @param {string} correctText - 正确的验证码文本
   * @returns {boolean} 验证结果
   */
  static verify(userInput, correctText) {
    if (!userInput || !correctText) {
      return false;
    }
    return userInput.toLowerCase() === correctText.toLowerCase();
  }
}

export default CaptchaUtil; 